// 添加以下配置
import { defineConfig } from 'vite';
import path from 'path';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  plugins: [vue()],
  server: {
    port: 3000,
    open: true,
    headers: {
      'X-Content-Type-Options': 'nosniff'
    },
    proxy: {
      '/api': {
        target: 'http://localhost:6666',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyRes', (proxyRes) => {
            // 强制清除所有可能的CORS源头（包括大小写变化）
            for (const key of Object.keys(proxyRes.headers)) {
              if (key.toLowerCase() === 'access-control-allow-origin') {
                delete proxyRes.headers[key];
              }
            }
            // 只设置一个允许的源
            proxyRes.headers['Access-Control-Allow-Origin'] = 'http://localhost:3000';
            // 添加必要的CORS响应头
            proxyRes.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS';
            proxyRes.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization';
            proxyRes.headers['Access-Control-Allow-Credentials'] = 'true';
          });
        }
      }
    }
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  },
  // 添加静态资源处理
  assetsInclude: ['**/*.jpg', '**/*.jpeg', '**/*.png'],
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
          @mixin cross-browser($property, $value) {
            #{$property}: $value;
            -webkit-#{$property}: $value;
            -moz-#{$property}: $value;
          }
        `
      }
    }
  }
})